package dev.ece.core.shader;

import java.io.InputStream;
import java.util.Map;

import org.lwjgl.opengl.GL20;

import dev.ece.core.texture.Texture;

public class DefaultTextureShader extends Shader {

	private static DefaultTextureShader DEFAULT_TEXTURE_SHADER;
	
	private int textureHandler;
	
	public static DefaultTextureShader getDefaultTextureShader() {
		if(DEFAULT_TEXTURE_SHADER == null) {
			DEFAULT_TEXTURE_SHADER = new DefaultTextureShader(
					DefaultTextureShader.class.getClassLoader().getResourceAsStream("shaders/ui/default.vert"), 
					DefaultTextureShader.class.getClassLoader().getResourceAsStream("shaders/ui/default.frag"));
		}
		return DEFAULT_TEXTURE_SHADER;
	}
	
	public DefaultTextureShader(InputStream vertexShaderInputStream, InputStream pixelShaderInputStream,
			Map<String, String> functions) {
		super(vertexShaderInputStream, pixelShaderInputStream, functions);
	}

	public DefaultTextureShader(InputStream vertexShaderInputStream, InputStream pixelShaderInputStream) {
		super(vertexShaderInputStream, pixelShaderInputStream);
	}

	public DefaultTextureShader(StringBuffer vertexShaderSource, StringBuffer pixelShaderSource,
			Map<String, String> functions) {
		super(vertexShaderSource, pixelShaderSource, functions);
	}

	public DefaultTextureShader(String vertexShaderSource, String pixelShaderSource) {
		super(vertexShaderSource, pixelShaderSource);
	}

	@Override
	protected void onBindAttribLocations(int program) {
		GL20.glBindAttribLocation(program, 0, "ece_Vertex");
		GL20.glBindAttribLocation(program, 1, "ece_TexCoord");
	}

	@Override
	protected void onInitializeHandler(int program) {
		textureHandler = GL20.glGetUniformLocation(program, "texture");
	}

	public void glTexture(Texture texture) {
		GL20.glUniform1i(textureHandler, texture.bind(0));
	}

	@Override
	public void clearup() {
		GL20.glDeleteShader(textureHandler);
		super.clearup();
	}
	
}
